// (C) Copyright to Kevin. Applied to MIT LICENSE

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/GameModeBase.h"

#include "../BlockWorld.h"
#include "TextureManager.h"
#include "BlockManager.h"
#include "ChunkActor.h"
#include "DataStructures.h"
#include "QueuedThreadPool.h"
#include "GameFramework/Actor.h"
#include "../Networking/NetworkClient.h"

#include "BlockEngineGameMode.generated.h"

/**
 * 
 */
UCLASS()
class BLOCKWORLD_API ABlockEngineGameMode : public AGameModeBase
{
	GENERATED_BODY()
	
	

private:
	static ABlockEngineGameMode* instance;

	UPROPERTY(EditAnywhere, Category = "BlockEngine")
		AActor* chunk_container;

	UPROPERTY(EditAnywhere, Category = "BlockEngine")
		UMaterialInterface* material;

	NetworkClient* network = NULL;

	TextureManager* texture_manager = NULL;
	BlockManager* block_manager = NULL;

	FQueuedThreadPool* threadpool = NULL;

	TArray<AChunkActor*> chunks;

public:
	// Sets default values for this actor's properties
	ABlockEngineGameMode();

protected:
	// Called when the game starts or when spawned
	virtual void StartPlay() override;
	virtual void BeginDestroy() override;

public:

	NetworkClient* GetClient();

	AActor* GetChunkContainer();
	UMaterialInterface* GetMaterial();

	FQueuedThreadPool* GetThreadPool();

	BlockManager* GetBlockManager();
	TextureManager* GetTextureManager();

	AChunkActor* FindChunk(const FChunkCoordinate& coordinate);

	static ABlockEngineGameMode* GetInstance();


private:
	void SpawnTestChunks(int x, int z);
	
	void RegisterChunk(AChunkActor* chunk);
	void UnregisterChunk(AChunkActor* chunk);

	friend class AChunkActor;
};
